<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace XWAM\Model;

/**
 * Description of RegistrationModel
 *
 * @author SoChishun
 */
class RegistrationModel extends AppbaseModel {

    protected $tableName = 't_porg_user';

    /**
     * 用户注册
     * @param array $data
     * @return array
     * @since 1.0 2016-9-1 SoChishun Added.
     */
    public function regist($data) {
        $validator = array(
            array('user_name', 'require', '用户无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('user_name', 'checkName', '用户名已被注册!', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
            array('password', 'require', '密码无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('repassword', 'require', '确认密码无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
        );
        unset($data['captcha']);
        if (!$this->validate($validator)->create($data)) {
            return $this->returnMsg(false, $this->getError());
        }
        if ($this->password != $this->repassword) {
            return $this->returnMsg(false, '两次密码输入不一致!');
        }
        unset($data['repassword']);
        $result = $this->add($data);
        return $this->returnMsg($result);
    }

    // 检测用户名是否重复 2016-4-8 SoChishun Added.
    function checkName($val) {
        $n = $this->where(array('user_name' => trim($val)))->count('id');
        return $n < 1;
    }
}
